//
//  MYXDateTimeHelper.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/11/8.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "MYXDateTimeHelper.h"

@implementation MYXDateTimeHelper
+ (NSString *)prettyDateWithTimestamp:(long long)timestamp
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp/1000];
    return [self prettyDateWithReference:date];
}

// 将NSDate转换为人性化提示
+ (NSString *)prettyDateWithReference:(NSDate *)reference
{
    float different = [[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSinceDate:reference];
    if (different < 0) {
        different = 0;
    }
    
    float dayDifferent = floor(different / 86400);
    
    int days   = (int)dayDifferent;
    
    // 今天
    if (dayDifferent <= 0) {
        if (different < 60) {
            return @"刚刚";
        }
        
        if (different < 120) {
            return @"1分钟前";
        }
        
        // 1h内
        if (different < 60 * 60) {
            return [NSString stringWithFormat:@"%d分钟前", (int)floor(different / 60)];
        }
        
        // 1h-2h
        if (different < 7200) {
            return [NSString stringWithFormat:@"1小时前"];
        }
        
        //
        if (different < 86400) {
            return [NSString stringWithFormat:@"%d小时前", (int)floor(different / 3600)];
        }
    }
    else if (days < 7) {
        return [NSString stringWithFormat:@"%d天前", days];
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    return [dateFormatter stringFromDate:reference];
}
//今天昨天 时间
+ (NSString *)prettyDateWithRefe:(NSDate *)referen{
    NSTimeInterval secondsPerDay = 24 * 60 * 60;
    NSDate *today = [[NSDate alloc] init];
    NSDate *tomorrow, *yesterday;
    
    tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
    yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
    
    // 10 first characters of description is the calendar date:
    NSString * todayString = [[today description] substringToIndex:10];
    NSString * yesterdayString = [[yesterday description] substringToIndex:10];
    NSString * tomorrowString = [[tomorrow description] substringToIndex:10];
    
    NSString * dateString = [[referen description] substringToIndex:10];
    
    
    if ([dateString isEqualToString:todayString])
    {
        return @"今天";
    } else if ([dateString isEqualToString:yesterdayString])
    {
        return @"昨天";
    }else if ([dateString isEqualToString:tomorrowString])
    {
        return @"明天";
    }
    else
    {
//        NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc] init];
//        [newDateFormatter setDateFormat:@"yyyy-MM-dd"];
//        NSDate *date = [newDateFormatter dateFromString:dateString];
//        NSString *startTimeStr1= [NSString stringWithFormat:@"%@月%@",[[newDateFormatter stringFromDate:date] componentsSeparatedByString:@"-"][0],[[newDateFormatter stringFromDate:date] componentsSeparatedByString:@"-"][2]];
    
        NSString *startTimeStr1 =[NSString stringWithFormat:@"%@月%@",[dateString componentsSeparatedByString:@"-"][1],[dateString componentsSeparatedByString:@"-"][2]];
        
        return startTimeStr1;
    }
    
    //    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //    [dateFormatter setDateFormat:@"MM-dd HH:mm"];
    //    return [dateFormatter stringFromDate:referen];
}
+ (NSString *)time:(NSDate *)referen{
    NSTimeInterval secondsPerDay = 24 * 60 * 60;
    NSDate *today = [[NSDate alloc] init];
    NSDate *tomorrow, *yesterday;
    
    tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
    yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
    
    NSString * dateString = [[referen description] substringToIndex:10];
    
    NSString *startTimeStr1 =[NSString stringWithFormat:@"%@%@%@",[dateString componentsSeparatedByString:@"-"][0],[dateString componentsSeparatedByString:@"-"][1],[dateString componentsSeparatedByString:@"-"][2]];
    return startTimeStr1;
    
}
// 格式化NSDate，去除年和秒等
+ (NSString *)simplifyDateTime: (NSString *)datetime
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:datetime];
    
    if (date == nil) {
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
        date = [dateFormatter dateFromString:datetime];
    }
    
    NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc] init];
    [newDateFormatter setDateFormat:@"MM-dd HH:mm"];
    return [newDateFormatter stringFromDate:date];
}

+ (long long)longlongDateTime: (NSString *)datetime
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:datetime];
    return [date timeIntervalSince1970] * 1000;
}
+ (NSString *)getMMSSFromSS:(NSString *)totalTime{
    NSInteger seconds = [totalTime integerValue];
    
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
    //format of second
    // NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@小时%@分",str_hour,str_minute];
    
    return format_time;
}
+(NSString *)timeWithTimeIntervalString:(NSString *)timeString{
    // 格式化时间
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"yyyy年MM月dd日 HH:mm"];
    NSLocale *zhLocale1 = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    [dateFormatter1 setLocale:zhLocale1];
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    NSString* dateString = [dateFormatter1 stringFromDate:date];
    return dateString;
}
//生日时间计算
+(NSString *)birthDayTimeWithIntervalString:(NSString *)timeString{
    // 格式化时间
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"yyyy年MM月dd日"];
    NSLocale *zhLocale1 = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    [dateFormatter1 setLocale:zhLocale1];
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    NSString* dateString = [dateFormatter1 stringFromDate:date];
    return dateString;
}
+(NSString *)restMoneyTimeWithTimeIntervalString:(NSString *)timeString{
    NSDate *reference = [NSDate dateWithTimeIntervalSince1970:timeString.integerValue];
    float different = [[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSinceDate:reference];
    float dayDifferent = floor(different / 86400);
    
    int days   = (int)dayDifferent;
    //格式化时间
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    NSString *dateFormat ;
    if (days > 365) {
        dateFormat = @"yyyy年MM月dd日 HH:mm";
    }else{
        dateFormat = @"MM月dd日 HH:mm";
    }
    [dateformatter setDateFormat:dateFormat];
    NSLocale *zhLocale1 = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    [dateformatter setLocale:zhLocale1];
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]];
    NSString* dateString = [dateformatter stringFromDate:date];
    return dateString;
}
@end
